require(["jquery",'public','weui'],function($,public,weui) {
    public.headertitle('我发布的',MODULE+'PublicJump/releaseAdd');
    public.footer(1);
    public.allocator();

    function getData(){
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: {type: 1,token: public.getSession('token')},
            url: MODULE+'Goods/home_goods',           
        }).success(function (res) {
            console.log(res);   
            var _html = '';
            if(res.code == 3){
                $.alert(res.msg,function(){
                    window.history.go(-1);
                });
                return false;
            }
            if(!res.data || !res.data.length){
            	$('.myrelease_main').addClass('myrelease_none');
            	return false;
            }
            res.data.forEach(function(i){
                _html += '<li class="myrelease_list_item">'
                        +            '<a class="myrelease_list_item_link clearfix" href="'+MODULE+'PublicJump/releaseInfo/id/'+i.goods_id+'">'
                        +                '<div class="myrelease_list_item_left">'
                        +                    '<img src="'+PUBLIC+i.goods_picture_path+'" />'
                        +                '</div>'
                        +                '<div class="myrelease_list_item_right">'
                        +                    '<p class="myrelease_list_item_txt">'+i.goods_name+'</p>'
                        +                    '<p class="myrelease_list_item_time">'+i.goods_updated_time+'</p>'
                        +                '</div>'
                        +            '</a>'
                        +			'<div class="btns" data-id="'+i.goods_id+'">'
                        +				'<div class="btns_btop clearfix">'
                        +					'<a class="btn_reset" href="javascript:;"><span>刷新</span></>'
                        +					'<a class="btn_edit" href="'+MODULE+'PublicJump/releaseEdit/id/'+i.goods_id+'"><span>编辑</span></>'
                        +					'<a class="btn_delete" href="javascript:;"><span>删除</span></>'
                        +				'</div>'
                        +			'</div>'
                        +        '</li>'; 
            });
            $('.myrelease_list').append(_html);
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    }

    getData();

    //页面加载完毕
    public.loaded();

    //刷新
    $('.myrelease_list').on('click','.btn_reset',function(event){
    	var $this = $(this);
    	if($this.hasClass('on')){
    		return false;
    	}
    	var id = $this.closest('.btns').data('id');
    	$this.addClass('on');
    	$.ajax({
            type: 'post',
            dataType: 'json',
            data: {id: id,token: public.getSession('token')},
            url: MODULE+'Goods/refresh_goods',           
        }).success(function (res) {
        	if(res.code == 0){
            	$.alert('刷新成功',function(){
            		window.location.href = MODULE+'PublicJump/myRelease/match/'+(Math.random()*100 >> 1);
            	}); 
            }else{
            	$.alert('刷新失败');
            }
            $this.removeClass('on');
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    });

    //删除
    $('.myrelease_list').on('click','.btn_delete',function(event){
    	var $this = $(this);
    	var id = $this.closest('.btns').data('id');
    	$.ajax({
            type: 'post',
            dataType: 'json',
            data: {id: id,token: public.getSession('token')},
            url: MODULE+'Goods/delete_goods',           
        }).success(function (res) {
        	if(res.code == 0){
            	$.alert('删除成功',function(){
            		$this.closest('.myrelease_list_item').remove();
            	});            	
            }else{
            	$.alert('删除失败');
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    });
})